package com.zrc.japktool.apk.xapk2apk;


import java.util.ArrayList;
import java.util.List;
import pxb.android.axml.Axml;
import pxb.android.axml.Axml.Node;
import pxb.android.axml.Axml.Node.Attr;
import pxb.android.axml.AxmlReader;
import pxb.android.axml.AxmlWriter;
import com.zrc.japktool.util.Zip4jUtil;

public class ManifestXmlUtil {

    public static void modifiyApkManifestXml(String xapkPath) throws Exception {
        byte[] androidManifestBytes = Zip4jUtil.readFileBytes(xapkPath, "AndroidManifest.xml");
        AxmlReader rd = new AxmlReader(androidManifestBytes);
        Axml axml = new Axml();
        rd.accept(axml);
        Axml.Node manifest = axml.findFirst("manifest");

        List<Attr> del_manifestNode_Attrs = new ArrayList<>();
        for (Node.Attr attr : manifest.attrs) {
            if (attr.name.equals("requiredSplitTypes")) {//删除requiredSplitTypes属性
                del_manifestNode_Attrs.add(attr);
            }
            if (attr.name.equals("splitTypes")) {//删除splitTypes属性
                del_manifestNode_Attrs.add(attr);
            }
        }

        for (Node.Attr attr : del_manifestNode_Attrs) {
            manifest.attrs.remove(attr);
        }

        Node application = manifest.findFirst("application");
        List<Node> delNodes = new ArrayList<>();
        for (Node child : application.children) {
            boolean needDel = false;
            if (child.name.equals("meta-data")) {
                for (Node.Attr attr : child.attrs) {
                    if (attr.name.equals("name") && attr.value.toString().equals("com.android.vending.splits.required")) {
                        needDel = true;
                        break;
                    }

                    if (attr.name.equals("name") && attr.value.toString().equals("com.android.stamp.source")) {
                        needDel = true;
                        break;
                    }

                    if (attr.name.equals("name") && attr.value.toString().equals("com.android.stamp.type")) {
                        needDel = true;
                        break;
                    }

                    if (attr.name.equals("name") && attr.value.toString().equals("com.android.vending.splits")) {
                        needDel = true;
                        break;
                    }

                    if (attr.name.equals("name") && attr.value.toString().equals("com.android.vending.derived.apk.id")) {
                        needDel = true;
                        break;
                    }

                }
            }

            if (needDel) {
                delNodes.add(child);
            }
        }
        for (Object delNode : delNodes) {
            application.children.remove(delNode);
        }

        //修改后xml覆盖写入apk的AndroidManifest.xml
        AxmlWriter wr = new AxmlWriter();
        axml.accept(wr);
        byte[] modifiedData = wr.toByteArray();
        Zip4jUtil.writeFileBytes(xapkPath, "AndroidManifest.xml", modifiedData,true);
    }

    public static void main(String... args) throws Exception {

    }
}
